package com.hjw.httpclient.async;

import com.hjw.httpclient.async.util.HttpClientUtils;
import lombok.Setter;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 异步HttpClient的业务类，在此类中发送异步HttpClient请求
 *
 * @author hjwStrart
 * @time 2022-11-30 20:29
 * @version: 1.0
 */
@Service
@Setter
public class MyService {

    private CountDownLatch countDownLatch ;


    public void myService() {
        MyCallBack callBack = new MyCallBack();
        String url = "http://localhost:8080/test";
        HttpUriRequest httpRequest = HttpClientUtils.getHttpRequest(url);
        for (int i = 1; i <= 2; i++) {
            HttpClientUtils.doGet(url, new MyRetryProxy(2, httpRequest, callBack, countDownLatch));
        }
        try {
            System.out.println("开始等待");
            countDownLatch.await(10, TimeUnit.SECONDS);
            long count = countDownLatch.getCount();
            if (count != 0) {
                System.out.println("剩余锁数量：" + count);
            }
            System.out.println("继续执行");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
